package mce.web.controller.model;

import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class FirstDataUtil {
	private String storeName="1909365071";
	private String sharedSecret = "32313333333036333334333337363532333238373334383039393639313436303937303439333337303536313631303130363434383635323230353830363232"; 
	private String fmtDate;
	private String timeZone;
	
	/**
	 * 
	 */
	public FirstDataUtil() {
	}
	/**
	 * @param storeName
	 * @param sharedSecret
	 * @param fmtDate
	 * @param timeZone
	 */
	public FirstDataUtil(String storeName, String sharedSecret,
			String fmtDate, String timeZone) {
		this.storeName = storeName;
		this.sharedSecret = sharedSecret;
		this.fmtDate = fmtDate;
		this.timeZone = timeZone;
	}
	public String createHash(String charge) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd-HH:mm:ss");
		TimeZone.setDefault(TimeZone.getTimeZone("Asia/Calcutta"));
		timeZone = "IST";
		Date now = new java.util.Date(System.currentTimeMillis());
		fmtDate = dateFormat.format(now);
		String stringToHash = storeName + fmtDate + charge + sharedSecret;
		return calculateHashFromHex(new StringBuffer(stringToHash));
		}
	private String calculateHashFromHex(StringBuffer buffer) {
		String algorithm = "SHA-256";
		MessageDigest messageDigest = null;
		try {
		messageDigest = MessageDigest.getInstance(algorithm);
		} catch (Exception e) {
		throw new IllegalArgumentException("Algorithm '" + algorithm +
		"' not supported");
		}
		StringBuffer result = new StringBuffer();
		StringBuffer sb = new StringBuffer();
		byte[] bytes = buffer.toString().getBytes();

		int byteLen = bytes.length;
		for (int i = 0; i < byteLen; i++) {
		byte b = bytes[i];
		sb.append(Character.forDigit((b & 240) >> 4, 16));
		sb.append(Character.forDigit((b & 15), 16));
		}
		buffer = new StringBuffer(sb.toString());
		messageDigest.update(buffer.toString().getBytes());
		byte[] message = messageDigest.digest();
		int messageLen = message.length;
		for (int j = 0; j < messageLen; j++) {
		byte b = message[j];
		String apps = Integer.toHexString(b & 0xff);
		if (apps.length() == 1) {
		apps = "0" + apps;
		}
		result.append(apps);
		}
		return result.toString();
		}
	/**
	 * @return the storeName
	 */
	public String getStoreName() {
		return storeName;
	}
	/**
	 * @param storeName the storeName to set
	 */
	public void setStoreName(String storeName) {
		this.storeName = storeName;
	}
	/**
	 * @return the sharedSecret
	 */
	public String getSharedSecret() {
		return sharedSecret;
	}
	/**
	 * @param sharedSecret the sharedSecret to set
	 */
	public void setSharedSecret(String sharedSecret) {
		this.sharedSecret = sharedSecret;
	}
	/**
	 * @return the fmtDate
	 */
	public String getFmtDate() {
		return fmtDate;
	}
	/**
	 * @param fmtDate the fmtDate to set
	 */
	public void setFmtDate(String fmtDate) {
		this.fmtDate = fmtDate;
	}
	/**
	 * @return the timeZone
	 */
	public String getTimeZone() {
		TimeZone.setDefault(TimeZone.getTimeZone("Asia/Calcutta"));
		timeZone = "IST";
		return timeZone;
	}
	/**
	 * @param timeZone the timeZone to set
	 */
	public void setTimeZone(String timeZone) {
		this.timeZone = timeZone;
	}
		
}
